package com.sp.scmdb.serviceImpl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.sp.scmdb.bean.AreaBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.AreaDao;
import com.sp.scmdb.dao.RelEmpAreaDao;
import com.sp.scmdb.entity.Area;
import com.sp.scmdb.service.AreaService;
import com.sp.scmdb.service.LogService;

@Service("areaService")
public class AreaServiceImpl implements AreaService {

	private AreaDao areaDao;
	private LogService logs;
	private RelEmpAreaDao empAreaDao;

	public void setEmpAreaDao(RelEmpAreaDao empAreaDao) {
		this.empAreaDao = empAreaDao;
	}

	@Required
	public void setLogs(LogService logs) {
		this.logs = logs;
	}

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public AreaBean queryAll(AreaBean areaBean, AreaBean oldAreaBean){
		List<Area> lists = Lists.newArrayList();
		if (areaBean.equals(oldAreaBean)) {
			lists = oldAreaBean.getAllList();
		} else {
			StringBuilder Hql = new StringBuilder("from Area where 1=1");
			if (StringUtils.isNotEmpty(areaBean.getName())) {
				Hql.append(" and name like '%" + areaBean.getName().trim() + "%'");
			}
			if (StringUtils.isNotEmpty(areaBean.getType())) {
				Hql.append(" and type='" + areaBean.getType() + "'");
			}
			lists = areaDao.findAreaByHql(Hql.toString());
		}
		if (CollectionUtils.isNotEmpty(lists)) {
			areaBean.setAllList(lists);
			areaBean.setList(lists.subList(areaBean.getCurrentRow(), areaBean.getLastRow()));
		}
		return areaBean;
	}

	@Transactional
	public String newArea(AreaBean bean, UserBean user) {
		String result = "3";
		StringBuffer Hql = new StringBuffer("from Area where 1=1 ");
		Hql.append("and name='").append(bean.getName()).append("'");
		List<Area> list = areaDao.queryHql(Hql.toString());
		if (CollectionUtils.isEmpty(list)) {
			Area area = new Area();
			area.setName(bean.getName());
			area.setEmpCount(bean.getEmpCount());
			area.setTimeLength(bean.getTimeLength());
			area.setType(bean.getType());
			area.setReaders(bean.getReaders());
			area.setAreaX(0);
			area.setAreaY(0);
			area.setPhone(bean.getPhone());
			areaDao.save(area);
			logs.addLog(user, "添加 区域：\""+area.getName()+"\"成功！");
			result = CommonParamter.CHECK_SUCCESS;
		} else {
			result = CommonParamter.CHECK_REPEAT;
		}
		return result;
	}

	@Transactional
	public String editArea(AreaBean areaBean, UserBean user) {
		String result = "3";
		if (StringUtils.isNotEmpty(areaBean.getId())) {
			StringBuffer Hql = new StringBuffer("from Area where 1=1 ");
			Hql.append("and id=").append(areaBean.getId()).append("");
			List<Area> list = areaDao.queryHql(Hql.toString());
			if (CollectionUtils.isNotEmpty(list)) {
				Area area = list.get(0);
				area.setName(areaBean.getName());
				area.setEmpCount(areaBean.getEmpCount());
				area.setTimeLength(areaBean.getTimeLength());
				area.setType(areaBean.getType());
				area.setReaders(areaBean.getReaders());
				area.setPhone(areaBean.getPhone());
				areaDao.update(area);
				logs.addLog(user, "修改 区域：\""+area.getName()+"\"成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		} else {
			result = CommonParamter.CHECK_FAIL;
		}
		return result;
	}

	@Transactional
	public String editAreaPosition(AreaBean areaBean, UserBean user) {
		String result = "3";
		if (StringUtils.isNotEmpty(areaBean.getId())) {
			StringBuffer Hql = new StringBuffer("from Area where 1=1 ");
			Hql.append("and id=").append(areaBean.getId()).append("");
			List<Area> list = areaDao.queryHql(Hql.toString());
			if (CollectionUtils.isNotEmpty(list)) {
				Area area = list.get(0);
				area.setAreaX(areaBean.getAreaX());
				area.setAreaY(areaBean.getAreaY());
				areaDao.update(area);
				logs.addLog(user, "修改 区域位置：\""+area.getName()+"\"成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		} else {
			result = CommonParamter.CHECK_FAIL;
		}
		return result;
	}

	@Transactional
	public String delArea(String[] arr, UserBean user) {
		String result = CommonParamter.CHECK_FAIL;
		Area area = new Area();
		String areaName = "";
		for (int i = 0; i < arr.length; i++) {
			area = areaDao.getById(Integer.parseInt(arr[i]));
			if (area != null) {
				areaName = area.getName();
				if (CollectionUtils.isNotEmpty(empAreaDao.queryHql("from RelEmpArea where idArea="+area.getId()))) {
					result = "10";
					break;
				} else {
					areaDao.delete(area);
					logs.addLog(user, "删除 区域：\""+areaName+"\"成功！");
					result = CommonParamter.CHECK_SUCCESS;
				}
			}
		}
		return result;
	}

	@Transactional(readOnly = true)
	public AreaBean queryAreaByID(String pid) {
		AreaBean areaBean = null;
		if (StringUtils.isNotEmpty(pid)) {
			Area area = areaDao.getById(Integer.parseInt(pid));
			if (area != null) {
				areaBean = new AreaBean();
				areaBean.setName(area.getName());
				areaBean.setEmpCount(area.getEmpCount());
				areaBean.setTimeLength(area.getTimeLength());
				areaBean.setReaders(area.getReaders());
				areaBean.setType(area.getType());
				areaBean.setId(pid);
				areaBean.setPhone(area.getPhone());
			}
		}
		return areaBean;
	}

	@Transactional(readOnly = true)
	public List<Area> queryAllArea() {
		return areaDao.queryAll();
	}

	@Required
	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}	
}
